package ihm_clarifiee.Presentation.model_table;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class model_table_backlog extends AbstractTableModel {
    private final List<String[]> element = new ArrayList<String[]>();

    private final String[] entetes = {"Histoire","Duree", "Desciption","Taches","Theme", "Etat"};

    public model_table_backlog() {
        super();
   
    }

    
    public int getRowCount() {
        return element.size();
    }

    public int getColumnCount() {
        return entetes.length;
    }

    public String getColumnName(int columnIndex) {
        return entetes[columnIndex];
    }



    public void add(String [] s) {
        element.add(s);

        fireTableRowsInserted(element.size() -1, element.size() -1);
    }

    public void remove(int rowIndex) {
        element.remove(rowIndex);

        fireTableRowsDeleted(rowIndex, rowIndex);
    }

  
    public Object getValueAt(int rowIndex, int columnIndex) {
     switch(columnIndex){
            case 0:
                return  element.get(rowIndex)[0];
            case 1:
                return  element.get(rowIndex)[1];
             case 2:
                return  element.get(rowIndex)[2];
             case 3:
             	return element.get(rowIndex)[3];
             case 4:
              	return element.get(rowIndex)[4];
             case 5:
               	return element.get(rowIndex)[5];
            default:
                return null; //Ne devrait jamais arriver
        }
    }
    
    
    public void update(int ligne, String nom, String desc, int duree, String taches ,String theme, int i_etat){
        
    	String etat = "";
     	if (i_etat == 0){etat = "A faire";}
    	if (i_etat == 1){etat = "En cours";}
    	if (i_etat == 2){etat = "En test";}
    	if (i_etat == 3){etat = "Fait";}
    	String[] s = {nom, String.valueOf(duree), desc, taches,theme, etat};
        element.set(ligne, s);
        fireTableDataChanged();
        this.setValueAt(nom, ligne, 0);
        this.setValueAt(String.valueOf(duree), ligne, 1);
        this.setValueAt(desc, ligne, 2);
        this.setValueAt(taches, ligne, 3);
        this.setValueAt(theme, ligne, 4);
        this.setValueAt(etat, ligne, 5);

        fireTableDataChanged();
        fireTableCellUpdated(ligne, 0);
        fireTableCellUpdated(ligne, 1);
        fireTableCellUpdated(ligne, 2);
        fireTableCellUpdated(ligne, 3);
        fireTableCellUpdated(ligne, 4);
        fireTableCellUpdated(ligne, 5);
    }
   public void up(int ligne){
        
        String nom1 = (String)getValueAt(ligne, 0);
        String duree1 = String.valueOf(getValueAt(ligne, 1));
        String desc1 = (String)getValueAt(ligne, 2);
        String taches1 = (String)getValueAt(ligne, 3);
        String theme1 = (String)getValueAt(ligne, 4);
        String etat1 = (String)getValueAt(ligne, 5);

        
        String[] temp1 = {nom1 , duree1 , desc1, taches1, theme1, etat1};
        
        String nom2 = (String)getValueAt(ligne-1, 0);
        String duree2 = String.valueOf(getValueAt(ligne-1, 1));
        String desc2 = (String)getValueAt(ligne-1, 2);
        String taches2 = (String)getValueAt(ligne-1, 3);
        String theme2 = (String)getValueAt(ligne-1, 4);
        String etat2 = (String)getValueAt(ligne-1, 5);

        String[] temp2 = {nom2 , duree2 , desc2, taches2, theme2, etat2};
        
        element.set(ligne, temp2);
        element.set(ligne-1, temp1);      
        fireTableDataChanged();
        
        this.setValueAt(nom1, ligne-1, 0);
        this.setValueAt(duree1, ligne-1, 1);
        this.setValueAt(desc1, ligne-1, 2);
        this.setValueAt(taches1, ligne-1, 3);
        this.setValueAt(theme1, ligne-1, 4);
        this.setValueAt(etat1, ligne-1, 5);

        
        this.setValueAt(nom2, ligne, 0);
        this.setValueAt(duree2, ligne, 1);
        this.setValueAt(desc2, ligne, 2);
        this.setValueAt(taches2, ligne, 3);
        this.setValueAt(theme2, ligne, 4);
        this.setValueAt(etat2, ligne, 5);

        
        fireTableDataChanged();
        
        fireTableCellUpdated(ligne, 0);
        fireTableCellUpdated(ligne, 1);
        fireTableCellUpdated(ligne, 2);
        fireTableCellUpdated(ligne, 3);
        fireTableCellUpdated(ligne, 4);
        fireTableCellUpdated(ligne, 5);

        
        fireTableCellUpdated(ligne-1, 0);
        fireTableCellUpdated(ligne-1, 1);
        fireTableCellUpdated(ligne-1, 2);
        fireTableCellUpdated(ligne-1, 3);
        fireTableCellUpdated(ligne-1, 4);
        fireTableCellUpdated(ligne-1, 5);

    }
    
        public void down(int ligne){
        
        String nom1 = (String)getValueAt(ligne, 0);
        String duree1 = String.valueOf(getValueAt(ligne, 1));
        String desc1 = (String)getValueAt(ligne, 2);
        String taches1 = (String)getValueAt(ligne, 3);
        String theme1 = (String)getValueAt(ligne, 4);
        String etat1 = (String)getValueAt(ligne, 5);

        String[] temp1 = {nom1 , duree1 , desc1, taches1, theme1, etat1};
        
        String nom2 = (String)getValueAt(ligne+1, 0);
        String duree2 = String.valueOf(getValueAt(ligne+1, 1));
        String desc2 = (String)getValueAt(ligne+1, 2);
        String taches2 = (String)getValueAt(ligne+1, 3);
        String theme2 = (String)getValueAt(ligne+1, 4);
        String etat2 = (String)getValueAt(ligne+1, 5);

        String[] temp2 = {nom2 , duree2 , desc2, taches2, theme2, etat2};
        
        element.set(ligne, temp2);
        element.set(ligne+1, temp1);      
        fireTableDataChanged();
        
        this.setValueAt(nom1, ligne+1, 0);
        this.setValueAt(duree1, ligne+1, 1);
        this.setValueAt(desc1, ligne+1, 2);
        this.setValueAt(taches1, ligne+1, 3);
        this.setValueAt(theme1, ligne+1, 4);
        this.setValueAt(etat1, ligne+1, 5);

        
        this.setValueAt(nom2, ligne, 0);
        this.setValueAt(duree2, ligne, 1);
        this.setValueAt(desc2, ligne, 2);
        this.setValueAt(taches2, ligne, 3);
        this.setValueAt(theme2, ligne, 4);
        this.setValueAt(etat2, ligne, 5);

        
        fireTableDataChanged();
        
        fireTableCellUpdated(ligne, 0);
        fireTableCellUpdated(ligne, 1);
        fireTableCellUpdated(ligne, 2);
        fireTableCellUpdated(ligne, 3);
        fireTableCellUpdated(ligne, 4);
        fireTableCellUpdated(ligne, 5);

             
        fireTableCellUpdated(ligne+1, 0);
        fireTableCellUpdated(ligne+1, 1);
        fireTableCellUpdated(ligne+1, 2);
        fireTableCellUpdated(ligne+1, 3);
        fireTableCellUpdated(ligne+1, 4);
        fireTableCellUpdated(ligne+1, 5);

        
    }
     
    
}
